package com.cai310.lottery.ticket.protocol.local.utils;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.apache.commons.lang.StringUtils;

import com.cai310.lottery.Constant;
import com.cai310.lottery.PlConstant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.support.ContentBean;
import com.cai310.lottery.support.el11to5.El11to5ContentBeanBuilder;
import com.cai310.lottery.support.el11to5.El11to5PlayType;
import com.cai310.lottery.support.pl.PlPlayType;

public  class EL11TO5CPUtil extends CPUtil{
	/**
	 *  复试已经处理了。单式还没处理
	 * @param lottery
	 * @param playType
	 * @return
	 */
	private static final String NUMCODE = ",";
	private static final String CONTENTCONNECTCODE = "-";
	private static final String DANSPLITCODE = ":";
	private static final String SINGSPILTCODE = "+";
	private static final NumberFormat TWO_NF = new DecimalFormat("00");
	@Override
	public String getCpLotteryId(Lottery lottery,Byte betType){
		return "15";
	}
	@Override
	public String getCpBetType(Ticket ticket){
		Byte betType = ticket.getBetType();
		return ""+betType;
	}
	@Override
	public String getCpPlayType(Ticket ticket){
		return ticket.getExtension();
		
	}
	@Override
	public String getBetContent(Ticket ticket){
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return ticket.getContent();
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return ticket.getContent();
		}
		return null;
	}
	protected String formatBetNum(String line,NumberFormat nf){
		StringBuffer sb = new StringBuffer();
		String[] arr = line.split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		for (int i = 0; i < arr.length; i++) {
			sb.append(nf.format(Integer.valueOf(arr[i]))).append(NUMCODE);
		}
		return sb.substring(0,sb.length()-NUMCODE.length());
	}
	public static ContentBean buildContentBean(Ticket ticket) throws DataException{
		String playTypeStr = ticket.getExtension();
		Byte betType = ticket.getBetType();
		El11to5PlayType el11to5PlayType =null;
		if(betType.equals(Byte.valueOf("0"))){
			el11to5PlayType = El11to5PlayType.NormalOne;
		}else if(betType.equals(Byte.valueOf("1"))){
			el11to5PlayType = El11to5PlayType.RandomTwo;
		}else if(betType.equals(Byte.valueOf("2"))){
			el11to5PlayType = El11to5PlayType.RandomThree;
		}else if(betType.equals(Byte.valueOf("3"))){
			el11to5PlayType = El11to5PlayType.RandomFour;
		}else if(betType.equals(Byte.valueOf("4"))){
			el11to5PlayType = El11to5PlayType.RandomFive;
		}else if(betType.equals(Byte.valueOf("5"))){
			el11to5PlayType = El11to5PlayType.RandomSix;
		}else if(betType.equals(Byte.valueOf("6"))){
			el11to5PlayType = El11to5PlayType.RandomSeven;
		}else if(betType.equals(Byte.valueOf("7"))){
			el11to5PlayType = El11to5PlayType.RandomEight;
		}else if(betType.equals(Byte.valueOf("8"))){
			el11to5PlayType = El11to5PlayType.ForeTwoGroup;
		}else if(betType.equals(Byte.valueOf("9"))){
			el11to5PlayType = El11to5PlayType.ForeTwoDirect;
		}else if(betType.equals(Byte.valueOf("10"))){
			el11to5PlayType = El11to5PlayType.ForeThreeGroup;
		}else if(betType.equals(Byte.valueOf("11"))){
			 el11to5PlayType = El11to5PlayType.ForeThreeDirect;
		}else{
			//报错
			throw new DataException("组合出票玩法出错");
		}
		if("1".equals(playTypeStr)){
			return El11to5ContentBeanBuilder.buildSingleContentBean(buildSingleStr(ticket.getContent()), el11to5PlayType);
		}else if("2".equals(playTypeStr)||"5".equals(playTypeStr)||"0".equals(playTypeStr)){
			if(el11to5PlayType.equals(El11to5PlayType.ForeThreeDirect)||el11to5PlayType.equals(El11to5PlayType.ForeTwoDirect)){//
				return El11to5ContentBeanBuilder.buildCompoundContentBean(buildDirectCompoundStr(ticket.getContent(),ticket.getUnits()), el11to5PlayType);
			}else{
				if(ticket.getContent().indexOf(DANSPLITCODE)!=-1){
					return El11to5ContentBeanBuilder.buildCompoundContentBean(buildCompoundStr(ticket.getContent(),ticket.getUnits()), el11to5PlayType);
				}else{
					//报错
					throw new DataException("组合出票内容出错");
				}
			}
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	public static El11to5PlayType getEl11to5PlayType(Ticket ticket) throws DataException{
		Byte betType = ticket.getBetType();
		El11to5PlayType el11to5PlayType =null;
		if(betType.equals(Byte.valueOf("0"))){
			el11to5PlayType = El11to5PlayType.NormalOne;
		}else if(betType.equals(Byte.valueOf("1"))){
			el11to5PlayType = El11to5PlayType.RandomTwo;
		}else if(betType.equals(Byte.valueOf("2"))){
			el11to5PlayType = El11to5PlayType.RandomThree;
		}else if(betType.equals(Byte.valueOf("3"))){
			el11to5PlayType = El11to5PlayType.RandomFour;
		}else if(betType.equals(Byte.valueOf("4"))){
			el11to5PlayType = El11to5PlayType.RandomFive;
		}else if(betType.equals(Byte.valueOf("5"))){
			el11to5PlayType = El11to5PlayType.RandomSix;
		}else if(betType.equals(Byte.valueOf("6"))){
			el11to5PlayType = El11to5PlayType.RandomSeven;
		}else if(betType.equals(Byte.valueOf("7"))){
			el11to5PlayType = El11to5PlayType.RandomEight;
		}else if(betType.equals(Byte.valueOf("8"))){
			el11to5PlayType = El11to5PlayType.ForeTwoGroup;
		}else if(betType.equals(Byte.valueOf("9"))){
			el11to5PlayType = El11to5PlayType.ForeTwoDirect;
		}else if(betType.equals(Byte.valueOf("10"))){
			el11to5PlayType = El11to5PlayType.ForeThreeGroup;
		}else if(betType.equals(Byte.valueOf("11"))){
			 el11to5PlayType = El11to5PlayType.ForeThreeDirect;
		}else{
			//报错
			throw new DataException("组合出票玩法出错");
		}
		return el11to5PlayType;
	}
	public static SalesMode getSalesMode(Ticket ticket) throws DataException{
		String playTypeStr = ticket.getExtension();
		if("1".equals(playTypeStr)){
			  return SalesMode.SINGLE;
		}else if("2".equals(playTypeStr)||"5".equals(playTypeStr)||"0".equals(playTypeStr)){
            return SalesMode.COMPOUND;
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	protected static String buildDirectCompoundStr(String content,Integer units){
		return units+DANSPLITCODE+content;
	}
	protected static String buildCompoundStr(String content,Integer units){
		String[] arr = content.split(DANSPLITCODE);
		if(StringUtils.isNotBlank(arr[0])){
			return units+DANSPLITCODE+arr[0]+";"+arr[1];
		}else{
			return units+DANSPLITCODE+arr[1];
		}
	}
	protected static String buildSingleStr(String content){
		content = content.replaceAll("\\"+SINGSPILTCODE, "\r\n");
		content = content.replaceAll(NUMCODE, Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		return content;
		
	}
	public static void main(String[] args) {
		EL11TO5CPUtil.buildSingleStr("03,07+01,08+03,09+02,10+01,02");
	}
	@Override
	public String getSpecialFlag(Ticket ticket) {
		return "";
	}
	@Override
	public String getUpdatePeriodNumber(Ticket ticket) {
		return ticket.getPeriodNumber().substring(2);
	}
	
}
